/**
 * 上传图片组件
 */
!function () {
    uploadOption = {
        url:"",
        name:"",
        file:"",
        vs:{},
        success:function (data) {},
        error:function (err) {},
    };
    uploader = function (option) {
        this.uploadOption = option
    };
    uploader.prototype.getType = function() {
        return this.uploadOption.file.type;
    };
    uploader.prototype.getSize = function() {
        return this.uploadOption.file.size;
    };
    uploader.prototype.getName = function() {
        return this.uploadOption.file.name;
    };
    uploader.prototype.doUpload = function() {
        var formData = new FormData();
        var $this = this;
        // add assoc key values, this will be posts values
        formData.append($this.uploadOption.name, $this.uploadOption.file, $this.getName());
        for(let key in $this.uploadOption.vs) {
            formData.append(key, $this.uploadOption.vs[key])
        }
        $.ajax({
            type: "POST",
            url: $this.uploadOption.url,
            xhr: function () {
                return $.ajaxSettings.xhr();
            },
            success: function (data) {
                $this.uploadOption.success(data)
            },
            error: function (err) {
                if(!!$this.uploadOption["error"]) {
                    $this.uploadOption.error(err)
                }
            },
            async: true,
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            timeout: 15000
        });
    };

    uploadImage = function (options, success, event) {
        if (event.target.files.length > 0) {
            let file = event.target.files[0];
            let upload = new uploader({
                url: "/images",
                name: "Images",
                vs: options,
                file: file,
                success: success,
            });
            upload.doUpload();
        }
    }
}();

/**
 * 事件驱动器
 */
!function(){
    dispatcher = {};
    dispatcher.events = {};

    // 监听事件
    dispatcher.listen = function (event, callback) {
        if(!dispatcher.events[event]) {
            dispatcher.events[event] = []
        }
        dispatcher.events[event].push(callback)
    };

    // 触发事件
    dispatcher.trigger = function(event, options) {
        let callbacks = dispatcher.events[event];
        if(!!callbacks) {
            for (let i = 0, j = callbacks.length; i < j; i++) {
                callbacks[i](options)
            }
        }
    }
}();

/**
 * 消息函数
 */
!function () {
    $.ajaxSettings = $.extend($.ajaxSettings, {
        complete: function (xhr) {
            if(xhr.status === 202) {
                let msg = JSON.parse(xhr.responseText);
                if(msg.hasOwnProperty("MsgType")){
                    dispatcher.trigger("message", {
                        typ: msg["MsgType"],
                        content: msg["MsgContent"],
                        hidden: false,
                    })
                }
            }
        }
    });

    alertWarning = function (msg) {
        dispatcher.trigger("message", {
            typ:"warning",
            content:msg,
            hidden:false,
        })
    };
    alertInfo = function (msg) {
        dispatcher.trigger("message", {
            typ:"info",
            content:msg,
            hidden:false,
        })
    };
    alertSuccess = function (msg) {
        dispatcher.trigger("message", {
            typ:"success",
            content:msg,
            hidden:false,
        })
    };
    alertDanger = function (msg) {
        dispatcher.trigger("message", {
            typ:"danger",
            content:msg,
            hidden:false,
        })
    }
}();